package com.hckjsec.website.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteNotify;
import com.hckjsec.website.service.IWebSiteNotifyService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 通知公告Controller
 * 
 * @author cyt
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/webnotify")
public class WebSiteNotifyController extends BaseController
{
    @Autowired
    private IWebSiteNotifyService webSiteNotifyService;

    /**
     * 查询通知公告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebSiteNotify webSiteNotify)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteNotify.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteNotify> list = webSiteNotifyService.selectWebSiteNotifyList(webSiteNotify);
        return getDataTable(list);
    }

    /**
     * 导出通知公告列表
     */
    @Log(title = "通知公告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebSiteNotify webSiteNotify)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webSiteNotify.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebSiteNotify> list = webSiteNotifyService.selectWebSiteNotifyList(webSiteNotify);
        ExcelUtil<WebSiteNotify> util = new ExcelUtil<WebSiteNotify>(WebSiteNotify.class);
        return util.exportExcel(list, "notify");
    }

    /**
     * 获取通知公告详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webSiteNotifyService.selectWebSiteNotifyById(id));
    }

    /**
     * 新增通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebSiteNotify webSiteNotify)
    {
        if(StringUtils.isNull(webSiteNotify.getPublishTime())){
            webSiteNotify.setPublishTime(DateUtils.getNowDate());
        }
        if (!"".equals(StringUtils.isHcNull(webSiteNotify.getContent(),""))){//解密
            webSiteNotify.setContent(Base64Converter.decode(webSiteNotify.getContent()));
        }else{
            webSiteNotify.setContent(webSiteNotify.getContent());
        }
        webSiteNotify.setDeptId(SecurityUtils.getDeptId());
        webSiteNotify.setOperator(SecurityUtils.getUserId());
        return toAjax(webSiteNotifyService.insertWebSiteNotify(webSiteNotify));
    }

    /**
     * 修改通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebSiteNotify webSiteNotify)
    {
        if(StringUtils.isNull(webSiteNotify.getPublishTime())){
            webSiteNotify.setPublishTime(DateUtils.getNowDate());
        }
        if (!"".equals(StringUtils.isHcNull(webSiteNotify.getContent(),""))){//解密
            webSiteNotify.setContent(Base64Converter.decode(webSiteNotify.getContent()));
        }else{
            webSiteNotify.setContent(webSiteNotify.getContent());
        }
        return toAjax(webSiteNotifyService.updateWebSiteNotify(webSiteNotify));
    }

    /**
     * 删除通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webSiteNotifyService.deleteWebSiteNotifyByIds(ids));
    }
}
